import datetime


def time_format(date):
    '''格式统一'''
    return date
#    return datetime.fromtimestamp(date)


def get_now():
    '''当前时间'''
    return datetime.datetime.now()


def date_timestamp(date):
    '''时间格式转时间戳'''
    date = time_format(date)
    return date.timestamp()


def date_strftime(date, strftime):
    '''时间转文本'''
    date = time_format(date)
    return date.strftime(strftime)


def date_timedelta(date1, date2, dtype):
    '''时间计算并转换为天，小时，分钟或秒'''
    date1 = time_format(date1)
    date2 = time_format(date2)
    if not isinstance(date1, datetime.datetime):
        return 0
    if not isinstance(date2, datetime.datetime):
        return 0
    duration = date2 - date1
    if dtype == "DAYS":
        return duration.days
    elif dtype == "HOUR":
        return duration.seconds/3600
    elif dtype == "SEC":
        return duration.seconds/60
    return duration.seconds


def date_strptime(date, strptime):
    '''文本转时间'''
    return datetime.datetime.strptime(date, strptime)

